<?php namespace Home\Controller; 

use Hdphp\Controller\Controller;
use Common\Model\Cate;
use Common\Model\Goods;
use Common\Model\GoodsAttr;
use Common\Model\TypeAttr;

//列表控制器
class ListController extends Controller{

	//构造函数
	public function __init()
	{
	}
	
	
	/**
	 * 市集页面
	 */
	public function market(){
		//思路：查询分类表，把相同pid的子类，标题为1维数组，子类为二维数组
		
		//1.查询分类表数据
		$cateModel = new Cate();
		$cate = $cateModel->where("pid=0")->get();
		$sonCate = $cateModel->where("pid != 0")->get();
		
		//2.把想同pid的放一起
		foreach ($cate as $k => $v) {
			foreach ($sonCate as $kk => $vv) {
				if($vv['pid'] == $v['cid']){
					$cate[$k]['value'][$kk]=$sonCate[$kk];
				}
			}
		}
//		p($cate);
		
		View::with('cate',$cate);
		View::make();
	}
	
    /**
	 * 新品页面
	 */
    public function news(){
    	//思路：先处理顶级分类，处理全部分类（获取商品表中全部的数据），
		
		//1.获取顶级分类
		$cateModel = new Cate();
		$topCate = $cateModel->where("pid=0")->orderBy('cid','ASC')->get();
		View::with('topCate',$topCate);
		
		//2.处理分类的商品信息
		$cid = Q('get.cid');
		$goodsData = $this->data($cid);
		View::with('goodsData',$goodsData);
		
       	View::make();
    }
	
	/**
	 * 处理商品分类的信息显示
	 * @return 商品的数据
	 */
	 public function data($cid){
	 	$cid = explode('_', $cid);
	 	$father = ['time','click','stock'];
	 	//如果$cid=0说明是取全部商品的信息，如果传过来其他的$cid,就取特定的商品数据
	 	$goodsModel = new Goods();
		$cataModel = new Cate();
		
		//分类查询新品,热卖,闪购的商品数据,在他们的cid前添加对应的字段名，新品根据time排序，热卖根据click，闪购根据stock
		foreach ($father as $k => $v) {//循环$father
			if($cid[0] == $v){//判断$cid[0]是否等于$father中的一种，这里可以把新品，热卖，闪购的商品分类
				$data = $goodsModel->orderBy("{$cid[0]}",'DESC')->limit(16)->get();
				if($cid[1] != 0){//这里都是一样的
					$allCate = $cataModel->get();
			 		$data = $goodsModel->sameCate($allCate,$cid[1]);
					$data = $goodsModel->whereIN('wsshop_category_cid',$data)->orderBy("{$cid[0]}",'DESC')->limit(16)->get();
				}
			}
		}
		return $data;
	 }
	
	
	/**
	 * 热卖页面
	 */
	 public function hot(){
	 	
		//1.获取顶级分类
		$cateModel = new Cate();
		$topCate = $cateModel->where("pid=0")->orderBy('cid','ASC')->get();
		View::with('topCate',$topCate);

		//2.处理分类的商品信息
		$cid = Q('get.cid');
		$goodsData = $this->data($cid);
		View::with('goodsData',$goodsData);


		
       	View::make();
	 }
	 
	 /**
	  * 闪购页面
	  */
	  public function rank(){
	 	
		//1.获取顶级分类
		$cateModel = new Cate();
		$topCate = $cateModel->where("pid=0")->orderBy('cid','ASC')->get();
		View::with('topCate',$topCate);
		
		//2.处理分类的商品信息-闪购
		$cid = Q('get.cid');
		$goodsData = $this->data($cid);
		View::with('goodsData',$goodsData);

		
       	View::make();
	 }
	  
	/**
	 * 第一级子类的页面
	 */
	 public function cate(){
	 	
		//当我进来时显示所有的商品，比如服装类型的商品-cate.php
		$cid = Q('get.cid');
		$goodsModel = new Goods();
		$cateModel = new Cate();
		$allCate = $cateModel->get();
		$cids = $goodsModel->sameCate($allCate, $cid);//这是获取当前分类的子类
		$cids[] = $cid;//把自己压入
		$cateGoodsData = $goodsModel->whereIN("wsshop_category_cid",$cids)->get();//查询当前分类和它自己的商品信息
		View::with('cateGoodsData',$cateGoodsData);
	 	
		
		//1.处理顶部分类（思路：获得对应的cid,查询分类表求出它子级和它下一级的子类信息，分配到页面）
		$cid = Q('get.cid',0,'intval');
		$fatherData = $cateModel->where("cid={$cid}")->first();
		$sonData = $cateModel->where("pid={$cid}")->get();
		View::with('fatherData',$fatherData);
		View::with('sonData',$sonData);
		
		//2.处理筛选栏
			//1>通过当前分类的cid获得所有子集的cid
			$cids = $cateModel->getSon($cateModel->orderBy('pid','ASC')->get(),$cid);
			$cids[] = $cid;
			
			//2.根据cid获得商品的gid
			$goodsModel = new Goods();
			$gids = $goodsModel->where("wsshop_category_cid IN(" . implode(',', $cids) . ")")->lists('gid');
			if($gids){
				//通过gid到wsshop_goods_attr表查询属性
				$goodsAttrModel = new GoodsAttr();
				$goodsAttr = $goodsAttrModel->where("wsshop_goods_gid IN(". implode(',', $gids) . ")")->groupBy('gavalue')->get();
				
				//把相同taid的属性放在一起
				$sameTemp = [];
				foreach ($goodsAttr as $v) {
					$sameTemp[$v['wsshop_type_attr_taid']][] = $v;
				}
				
				//组合名字和值
				$typeAttrModel = new TypeAttr();
				$finalAttr = [];
				foreach ($sameTemp as $taid => $v) {
					$finalAttr[]=[
						'taname'=>$typeAttrModel->where("taid={$taid}")->pluck('taname'),
						'value'=> $v
					];
				}
				View::with('finalAttr',$finalAttr);
				
				//筛选效果
				//1.地址栏效果
				$num = count($finalAttr);
				$param = isset($_GET['param']) ? explode('_', $_GET['param']) : array_fill(0,$num,0);
				View::with('param',$param);
				
				//2.筛选操作，最终获得商品gid
				$finalGids = $this->filter($param,$gids);
				
			}else{
				View::with('finalAttr',[]);
			}
			
		//3.进去之后
//		$param = Q('get.param');
//		p($param);
			
		View::make();
	 }

	//筛选操作，最终获得商品gid
	private function filter($param,$cidGids){
		$goodsAttrModel = new GoodsAttr();
		$gidArr = [];
		foreach ($param as $gaid) {
			//如果点击的不是'不限'的时候
			//根据gaid得到商品的gid
			if($gaid){
				//查询得到商品属性的名称
				$gavalue = $goodsAttrModel->where("gaid={$gaid}")->pluck('gavalue');
				//通过名称查询到gid
				$gidArr[] = $goodsAttrModel->where(['gavalue'=>$gavalue])->lists('wsshop_goods_gid');
			}
			
		}
		
		//如果用户点击的不全部是‘不限’的时候
		if($gidArr){
			$gid = $gidArr[0];
			foreach ($gidArr as $v) {
				$gid = array_intersect($v, $gid);
			}
			//因为$gid有可能是其他分类的gid,那么为了确保是当前分类,再次取交集
            $finalGids = array_intersect($gid,$cidGids);
		}else{//如果是全部是'不限'的时候
			$finalGids = $cidGids;
		}
		return $finalGids;
	}
	
	
	
	
	
	
	
	
}
